
    public class MyFraction {
        private int numer;
        private int denom;
        public MyFraction(){
            this.numer = 0;
            this.denom = 1;
        }
        public MyFraction(int numer) {
            this.numer = numer;
            this.denom = 1;
        }
        public MyFraction(int numer, int denom) {
            this.numer = numer;
            this.denom = denom;
            normalize();
        }

        public int getNumer() {
            return numer;
        }

        public int getDenom() {
            return denom;
        }

        @Override
        public String toString() {
            return "MyFraction{" +
                    "numer=" + numer +
                    ", denom=" + denom +
                    '}';
        }
        public MyFraction add(MyFraction fraction){
            MyFraction temp = new MyFraction();
            temp.numer = numer * fraction.denom + denom * fraction.numer;
            temp.denom = denom * fraction.denom;
            return temp;
        }

        public MyFraction subtract(MyFraction fraction){
            MyFraction temp = new MyFraction();
            temp.numer = numer * fraction.denom - denom * fraction.numer;
            temp.denom = denom * fraction.denom;
            return temp;
        }

        public MyFraction multiply(MyFraction fraction){
            MyFraction temp = new MyFraction();
            temp.numer = numer * fraction.numer;
            temp.denom = denom * fraction.denom;
            temp.normalize();
            return temp;
        }

        public MyFraction divide(MyFraction fraction){
            MyFraction temp = new MyFraction();
            temp.numer = numer * fraction.denom;
            temp.denom = denom * fraction.numer;
            temp.normalize();
            return temp;
        }

        private int gcd(int n,int m){
            int gcd = 1;
            for (int k=1;k<=n&&k<=m;k++){
                if(n%k ==0&&m%k==0){
                    gcd = k;
                }
            }
            return gcd;
        }

        public void normalize(){
            if(denom==0){
                throw new RuntimeException("分母不为零");
            }
            if(denom<0){
                denom=-denom;
                numer=-numer;
            }
            int divisor = gcd(Math.abs(numer),Math.abs(denom));
            numer = numer/divisor;
            denom = denom/divisor;
        }
}






